package org.divineryang.utils;

import org.divineryang.model.entity.system.SysMenu;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program new-bee
 * @Description 封装树行结构
 * @Author Diviner Yang
 * @Date 2024/04/22 7:59
 * @Version 1.0
 */
public class MenuHelp {
    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList){
        List<SysMenu> trees = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getParentId().longValue()==0){
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    private static SysMenu findChildren(SysMenu sysMenu, List<SysMenu> treeNodes) {
        sysMenu.setChildren(new ArrayList<SysMenu>());
        for (SysMenu it : treeNodes) {
            if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
                sysMenu.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysMenu;
    }
}
